package com.harjeethj.wheresmymaggi;

import web.product;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Fetch_product_details extends Activity {
	
	String pName=null;
	String url;
	product p1;
	
	TextView txt_location;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fetch_product_details);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		   //  pName = extras.getString("p_Name");
		     p1=(product)extras.getSerializable("Object");
		}
		
		/// I replaced this section of code to main activity
		
		/*web.get_product_details get_details = new web.get_product_details();
		p1=get_details.getProduct_details(pName);
		
		*/
		
		TextView txt_pname = (TextView) findViewById(R.id.txt_product_name_confirm);
		txt_pname.setText(p1.getName());
		txt_location = (TextView) findViewById(R.id.txt_map_location);

		txt_location.setText(p1.getLattitude() + " , " + p1.getLongitude());

		url = "http://maps.google.com/maps?q=" + p1.getLattitude() + ","
				+ p1.getLongitude();

		Button btn_seeonmaps = (Button) findViewById(R.id.btn_see_on_maps);
		btn_seeonmaps.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);

			}
		});
	       
	       
	       
	       
		final Button btn_get_directions = (Button) findViewById(R.id.btn_get_direction);
		btn_get_directions.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				btn_get_directions.setText("Fetching your location");
				ProgressBar loading_location = (ProgressBar) findViewById(R.id.bar_location_loading);
				loading_location.setVisibility(View.VISIBLE);

				/***************************************
	        	    * 
	        	    * 
	        	    */

				gotodirections();

				/*********************
	        	    * 
	        	    * 
	        	    * 
	        	    * 
	        	    */

			}
		});

	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.fetch_product_details, menu);
		return true;
	}
	
	
	public void gotodirections(){
		
		 final LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
         LocationListener locationListener = new LocationListener() {
 	           
         	
         	

     		public void onLocationChanged(Location location) {
     			Context context = getApplicationContext();
     		    //https://maps.google.com/?saddr=34.052222,-118.243611&daddr=37.322778,-122.031944
     			locationManager.removeUpdates(this);
 			    url= "http://maps.google.com/maps?saddr=" + location.getLatitude()+","+location.getLongitude()+",&daddr="+p1.getLattitude()+","+p1.getLongitude();	        			   
 			    Intent i = new Intent(Intent.ACTION_VIEW);
           	  	i.setData(Uri.parse(url));
           	  	startActivity(i); 
     			
     			
     			
     			
 			    }
     		
     		

 			@Override
 			public void onProviderDisabled(String provider) {
 				// TODO Auto-generated method stub
 				
 			}

 			@Override
 			public void onProviderEnabled(String provider) {
 				// TODO Auto-generated method stub
 				
 			}

 			@Override
 			public void onStatusChanged(String provider, int status,
 					Bundle extras) {
 				// TODO Auto-generated method stub
 				
 			}
 			
         };
         
         locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 400, 0, locationListener);
		
		
		
	}
	
	

}
